/* eslint-disable no-console */
const nunjucks = require('nunjucks')

function renderIndexName2(index: any, tableName: string): string {
	if (index.name) {
		return index.name
	}
	return `IK_${tableName}_${index.columns.replace(/[()\s]/g, '').replace(/,/g, '_')}`
}
const model = {
	tables: [
		{
			name: 'departments',
			comment: 'The department table.',
			primaryKey: {
				columns: 'DepartmentID',
				cluster: true,
			},
			indexes: [],
			foreignKeys: [
				{
					columns: 'ManagerID',
					refTable: 'employees',
					refColumns: 'EmployeeID',
					options: '',
				},
				{
					columns: 'ParentID',
					refTable: 'departments',
					refColumns: 'DepartmentID',
					options: '',
				},
			],
			columns: [
				{
					label: 'Department ID',
					name: 'DepartmentID',
					type: 'int auto_increment',
					nullable: false,
					default: '',
					comment: '',
				},
				{
					label: 'Department Name',
					name: 'DepartmentName',
					type: 'varchar(50)',
					nullable: false,
					default: '',
					comment: '',
				},
				{
					label: 'Parent Department',
					name: 'ParentID',
					type: 'int',
					nullable: true,
					default: '',
					comment: '',
				},
				{
					label: 'Manager',
					name: 'ManagerID',
					type: 'int',
					nullable: true,
					default: '',
					comment: '',
				},
			],
		},
		{
			name: 'employees',
			comment: 'employees',
			primaryKey: {
				columns: 'EmployeeID',
				cluster: false,
			},
			indexes: [
				{
					columns: 'FirstName, LastName',
					cluster: true,
					unique: false,
				},
				{
					columns: 'LastName',
					cluster: false,
					unique: false,
				},
			],
			foreignKeys: [
				{
					columns: 'DepartmentID',
					refTable: 'departments',
					refColumns: 'DepartmentID',
					options: '',
				},
			],
			columns: [
				{
					label: 'EmployeeID',
					name: 'EmployeeID',
					type: 'int auto_increment',
					nullable: false,
					default: '',
					comment: '',
				},
				{
					label: 'Last Name',
					name: 'LastName',
					type: 'varchar(50)',
					nullable: false,
					default: '',
					comment: '',
				},
				{
					label: 'First Name',
					name: 'FirstName',
					type: 'varchar(50)',
					nullable: false,
					default: '',
					comment: '',
				},
				{
					label: 'Department',
					name: 'DepartmentID',
					type: 'int',
					nullable: false,
					default: '',
					comment: '',
				},
			],
		},
	],
}

function toSQL(value: any): string {
	let sSQL: string

	const typeName = typeof value

	if (typeName === 'number') {
		sSQL = value
	} else if (typeName === 'object') {
		sSQL = `'${value as Date}'`
	} else if (typeName === 'boolean') {
		sSQL = (value as boolean) ? '1' : '0'
	} else {
		sSQL = `'${(value as string).replace("'", "''")}'`
	}
	return sSQL
}
// nunjucks.configure()
const env = new nunjucks.Environment(
	new nunjucks.FileSystemLoader('C:/0N/github/db-modeling-markdown/src/dialects/mariadb'),
	{
		autoescape: false,
		trimBlocks: true,
		lstripBlocks: false,
	},
)

env.addFilter('toSQL', toSQL)
env.addFilter('indexName', renderIndexName2)
console.log(env.render('sql-create-tables.njk', model))
// console.log(env.render('sql-drop-tables.njk', model))
// console.log(env.render('sql-create-tables-fk.njk', model))
